package tianqi.service.order.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import tianqi.Dao.order.OrderDao;
import tianqi.Dao.user.UserDao;
import tianqi.entity.Order;
import tianqi.entity.OrderDetail;
import tianqi.entity.ShareOrder;
import tianqi.entity.UserY;
import tianqi.service.order.OrderService;
import tianqi.service.shop.impl.ShopCarServiceImpl;
import tianqi.utils.TQMoneySmallException;
import tianqi.utils.TQStockException;

@Service
public class OrderServiceImpl implements OrderService {
	
	@Autowired
	private OrderDao orderDao;
	
	@Autowired
	private UserDao userDao;
	
	@Autowired
	private ShopCarServiceImpl shopCarServiceImpl;
	
	public static String error="您的订单已成功提交，请尽快付款！ <span style='margin-left:20px; font-size:12px;'>（未及时付款，再次支付时可能会出现商品缺货的情况。）</span>";

	@Override
	public String saveOrder(Order o,List<OrderDetail> od,HttpServletRequest req, HttpServletResponse res) throws TQStockException{
		orderDao.saveOrder(o);
		Map<String, String> map=new HashMap<String,String>();
		for (OrderDetail orderDetail : od) {
			orderDetail.setOrderid(o.getOrderId().toString());
			orderDao.saveOrderDetail(orderDetail);
			map.put("spec", orderDetail.getProSpec());
			map.put("specid", orderDetail.getProId());
			map.put("stock", orderDetail.getOnum());
			Integer updateStock = orderDao.queryStock(map);
			if (updateStock == 0) {
				error="抱歉！ "+orderDetail.getProCall()+" 库存不足";
				
				System.out.println("抱歉！ "+orderDetail.getProCall()+" 库存不足 ：：：id "+orderDetail.getProId() +" spec:"+orderDetail.getProSpec());
				throw new TQStockException("id: "+orderDetail.getProId()+" 库存不足");
			}
			
			
		}
		//订单生成后，清空购物车
		shopCarServiceImpl.clearCar(req, res);
		
		return "true";
		
	}

	@Override
	public List<Order> queryOrderByUid(UserY user) {
		
		return orderDao.queryOrderByUid(user);
	}

	@Override
	public Double queryMoneyByUid(UserY map) {
		return orderDao.queryMoneyByUid(map);
	}

	@Override
	public void deleteOrderById(Order order) {
		orderDao.deleteOrderById(order);
		orderDao.deleteOrderDetailById(order);
		
	}

	@Override
	public void savePay(String orderId,UserY usery) throws TQStockException,TQMoneySmallException {
		if (orderId == "" || orderId ==null || orderId==" ") {
			return;
		}
		String[] split=new String[]{};
		if (orderId.indexOf(",")>0) {
			split= orderId.split(",");
		}else{
			split=new String[]{orderId};
		}
		Order order=new Order();
		for (String string : split) {
			order.setOrderId(Integer.valueOf(string));
			List<OrderDetail> queryOrderDetail = orderDao.queryOrderDetail(order);
			for (OrderDetail orderDetail : queryOrderDetail) {
				Integer updateStock = orderDao.updateStock(orderDetail);
				if (updateStock == 0) {
					error="抱歉！ "+orderDetail.getProCall()+" 库存不足";
					System.out.println("id: "+orderDetail.getProId()+" 库存不足");
					throw new TQStockException("id: "+orderDetail.getProId()+" 库存不足");
				}
				usery.setMoney(orderDetail.getMoney());
				Integer updateMoney = userDao.updateMoney(usery);
				if (updateMoney == 0) {
					error="抱歉！ "+usery.getUname()+" 您的账户余额不足 订单："+order.getOrderId()+"无法提交";
					throw new TQMoneySmallException("抱歉！ "+usery.getUname()+" 您的账户余额不足");
				}
			}
			
			Integer updateOrderState = orderDao.updateOrderState(order);
			
			
		}
		
		
		
		
	}

	@Override
	public List<OrderDetail> queryOrderDetail(Order order) {
		return orderDao.queryOrderDetail(order);
	}

	@Override
	public Integer saveShareOrder(ShareOrder order) {
		
		return orderDao.saveShareOrder(order);
	}

	@Override
	public List<ShareOrder> querySoSByUname(UserY order) {
		// TODO Auto-generated method stub
		return orderDao.querySoSByUname(order);
	}



}
